package com.teichmann;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.teichmann.bo.Car;
import com.teichmann.core.targets.online.revolution.AutoCarro;
import com.teichmann.core.targets.online.revolution.SulRevendas;
import com.teichmann.core.targets.online.revolution.BasicPage;

@SuppressWarnings("serial")
public class ExtractDataServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {

		execute(req, resp);
	}

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, ServletException {

		execute(req, resp);

	}

	private void execute(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		if (req.getSession().getAttribute("pages") != null){
			List<Car> cars = new ArrayList<Car>();
			List<BasicPage> pages  = (List<BasicPage>) req.getSession().getAttribute("pages");
			
			BasicPage b = null;
			for(BasicPage p : pages){
				/*if (p instanceof SulRevendas){
					b = new com.teichmann.core.targets.online.revolution.SulRevendas();
					b.setUrls(p.getUrls());
				}
				else if (p instanceof AutoCarro){
					b = new com.teichmann.core.targets.online.revolution.AutoCarro(); 
					b.setUrls(p.getUrls());
				}*/
				cars.addAll(p.extractInformation());
			}
			
			req.getSession().setAttribute("cars", cars);
			
			
		}
		else{
			log("Session is null");
		}
		
	}

}
